using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Xml;

using EnvDTE;
using EnvDTE80;

using Microsoft.VisualStudio.VCProject;
using Microsoft.VisualStudio.VCProjectEngine;

namespace EEst.Tools.VsPackageNMakeAdapter
{
    public partial class UserControlBuildXCMakefile : UserControl
    {
        public Project Target
        {
            get { return _target; }
            set { _target = value; }
        }
        private Project _target;
        public string ConfigFile
        {
            get { return _configFile; }
            set { _configFile = value; }
        }
        private string _configFile;
        public string MakeFile
        {
            get { return _makeFile; }
            set { _makeFile = value; }
        }
        private string _makeFile;

        public UserControlBuildXCMakefile()
        {
            InitializeComponent();

            
        }



        private void UserControlBuildXCMakefile_Load(object sender, EventArgs e)
        {

        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            generator.ClearSrcFiles();
            for (int i = 0; i < checkedListBoxTargetFiles.CheckedItems.Count; i++)
            {
                string srcFile = checkedListBoxTargetFiles.CheckedItems[i] as string;
                generator.AddSrcFile(srcFile);
            }
            generator.ClearIncDirs();
            string[] incDirs = textBoxIncDirs.Text.Split(new char[] { ';' });
            for (int i = 0; i < incDirs.Length; i++)
            {
                generator.AddIncDir(incDirs[i]);
            }

            generator.Generate();
        }

        private VCProjectHelper helper = null;
        private XCMakefileGenerator generator = null;
        private void UserControlBuildXCMakefile_EnabledChanged(object sender, EventArgs e)
        {
            if (!this.Enabled)
            {
                return;
            }
            if (Target == null || ConfigFile == null || MakeFile == null)
            {
                return;
            }
            checkedListBoxTargetFiles.Items.Clear();
            helper = new VCProjectHelper(_target.FullName);
            FileInfo fi = new FileInfo(_target.FullName);
            generator = new XCMakefileGenerator(fi.DirectoryName,_makeFile, _configFile);

            string[] srcFiles = helper.SourceFiles;

            string[] registedSrcFiles = generator.GetSrcFiles();
            int registed = registedSrcFiles.Length;
            for (int i = 0; i < srcFiles.Length; i++)
            {
                string fileName = srcFiles[i].Substring(2);
                checkedListBoxTargetFiles.Items.Add(fileName);
                for (int j = 0; j < registed; j++)
                {
                    if (registedSrcFiles[j].CompareTo(fileName) == 0)
                    {
                        checkedListBoxTargetFiles.SetItemChecked(i, true);
                        registed--;
                        registedSrcFiles[j] = registedSrcFiles[registed];
                        break;
                    }
                }
                if (registedSrcFiles.Length == 0)
                {
                    checkedListBoxTargetFiles.SetItemChecked(i, true);
                }
            }

            StringBuilder builder = new StringBuilder();
            string[] registedIncDirs = generator.GetIncDirs();
            for (int i = 0; i < registedIncDirs.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append(";");
                }
                builder.Append(registedIncDirs[i]);
            }
            textBoxIncDirs.Text = builder.ToString();

        }

        private void buttonAddIncDirs_Click(object sender, EventArgs e)
        {
            FileInfo fi = new FileInfo(_target.FullName);
            folderBrowserDialogInc.SelectedPath = fi.DirectoryName;
            if (folderBrowserDialogInc.ShowDialog() == DialogResult.OK)
            {
                if (!String.IsNullOrEmpty(textBoxIncDirs.Text))
                {
                    textBoxIncDirs.Text += ";";
                }

                string selected = folderBrowserDialogInc.SelectedPath;


                textBoxIncDirs.Text += GetRelativePath(fi.DirectoryName, selected);
            }
        }

        private string GetRelativePath(string root, string target)
        {
            string[] rootFrags = root.Split(new char[] { '\\' });
            string[] targetFrags = target.Split(new char[] { '\\' });
            int index = 0;
            while (index < rootFrags.Length && index < targetFrags.Length)
            {
                if (rootFrags[index].CompareTo(targetFrags[index]) != 0)
                {
                    break;
                }
                index++;
            }
            string path = "";
            for (int i = index; i < rootFrags.Length; i++)
            {
                if (path.CompareTo("") != 0)
                {
                    path += "\\";
                }
                path += "..";
            }
            for (int i = index; i < targetFrags.Length; i++)
            {
                path += "\\" + targetFrags[i];
            }
            if (path.EndsWith(".."))
            {
                path += @"\.";
            }
            return path;
            
        }
    }
}
